# OpenHarmony交叉编译工具链配置（优化版）
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR aarch64)

# SDK路径
set(WS_ROOT /home/diemit/ssd/ros2-humble-cross-compile)
set(OHOS_SDK_ROOT ${WS_ROOT}/ohos-robot-toolchain/linux/native)

# 指定编译器
set(CMAKE_C_COMPILER ${OHOS_SDK_ROOT}/llvm/bin/clang)
set(CMAKE_CXX_COMPILER ${OHOS_SDK_ROOT}/llvm/bin/clang++)
set(CMAKE_LINKER ${OHOS_SDK_ROOT}/llvm/bin/ld.lld) 

# 设置sysroot
set(CMAKE_SYSROOT ${OHOS_SDK_ROOT}/sysroot)

# set the toolchain config.
set(TOOLCHAIN_ROOT_PATH "${OHOS_SDK_ROOT}/llvm")
set(TOOLCHAIN_BIN_PATH  "${OHOS_SDK_ROOT}/llvm/bin")

set(CMAKE_SYSROOT "${OHOS_SDK_ROOT}/sysroot")

set(CMAKE_LIBRARY_ARCHITECTURE "${OHOS_TOOLCHAIN_NAME}")
list(APPEND CMAKE_SYSTEM_LIBRARY_PATH "${CMAKE_SYSROOT}/vendor/lib/${OHOS_TOOLCHAIN_NAME}")
list(APPEND CMAKE_SYSTEM_LIBRARY_PATH "/usr/lib/${OHOS_TOOLCHAIN_NAME}")
set(CMAKE_C_COMPILER "${TOOLCHAIN_BIN_PATH}/clang${HOST_SYSTEM_EXE_SUFFIX}")
set(CMAKE_CXX_COMPILER "${TOOLCHAIN_BIN_PATH}/clang++${HOST_SYSTEM_EXE_SUFFIX}")

set(OHOS_AR "${TOOLCHAIN_BIN_PATH}/llvm-ar${HOST_SYSTEM_EXE_SUFFIX}")
set(OHOS_RANLIB "${TOOLCHAIN_BIN_PATH}/llvm-ranlib${HOST_SYSTEM_EXE_SUFFIX}")
set(CMAKE_AR                "${OHOS_AR}" CACHE FILEPATH "Archiver")
set(CMAKE_RANLIB            "${OHOS_RANLIB}" CACHE FILEPATH "Ranlib")
set(UNIX TRUE CACHE BOOL FROCE)

# 解决wchar_t类型冲突
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__WCHAR_TYPE__='unsigned int' -D_WCHAR_T_DEFINED -D__OHOS__ -D__MUSL__ -target aarch64-linux-ohos -fPIC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__WCHAR_TYPE__='unsigned int' -D_WCHAR_T_DEFINED -D__OHOS__ -D__MUSL__ -target aarch64-linux-ohos -fPIC")
set(CMAKE_ASM_COMPILER_TARGET "aarch64-linux-ohos")

# 关键：指定启动文件和GCC运行时库路径
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -B${CMAKE_SYSROOT}/usr/lib/aarch64-linux-ohos")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -B${CMAKE_SYSROOT}/usr/lib/aarch64-linux-ohos")

# 链接器选项（移除冗余的 -lgcc，使用 LLVM 内置运行时）
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} \
  -Wl,--sysroot=${CMAKE_SYSROOT} \
  -L${CMAKE_SYSROOT}/usr/lib/aarch64-linux-ohos \
  -L${OHOS_SDK_ROOT}/llvm/lib \
  -Wl,--no-as-needed -lpthread -ldl -lm -fopenmp \
  -Wl,-allow-shlib-undefined \
")

set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${CMAKE_EXE_LINKER_FLAGS}")

# 告诉CMake不要检查编译器的完整性
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")

# 调整查找路径
set(CMAKE_FIND_ROOT_PATH "${OHOS_SDK_ROOT}/sysroot")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

# 指定主机 Python 环境
set(PYTHON_EXECUTABLE /home/diemit/anaconda3/envs/ros2/bin/python3 CACHE STRING "Python executable" FORCE)
set(PYTHON_INCLUDE_DIR "${CMAKE_SYSROOT}/vendor/include/python3.12" CACHE PATH "Python include directory" FORCE)
set(PYTHON_LIBRARY ${CMAKE_SYSROOT}/vendor/lib/libpython3.12.so CACHE FILEPATH "Python library" FORCE)

# 指定ROS 2主机路径
set(ROS2_HOST_DIR ${WS_ROOT}/humble/install)
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} ${ROS2_HOST_DIR})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${ROS2_HOST_DIR}/share/cmake/Modules)

# 允许优先查找主机系统的ROS 2包
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH)
list(APPEND CMAKE_SYSTEM_LIBRARY_PATH "${ROS2_HOST_DIR}/lib")


# 从链接器参数中移除主机路径（如/opt/ros/foxy/lib）
string(REPLACE "/opt/ros/humble/lib" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
string(REPLACE "/opt/ros/humble/lib" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")

# 添加OHOS平台定义
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_CROSSCOMPILING TRUE)

# 线程库配置（修正为 pthread）
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
set(THREADS_PTHREAD_ARG "TRUE" CACHE STRING "Forcibly set by toolchain" FORCE)
set(CMAKE_THREAD_LIBS_INIT "-lpthread")

# 添加架构特定的头文件路径
set(ARCH_SPECIFIC_INCLUDE "${OHOS_SDK_ROOT}/sysroot/usr/include/${CMAKE_SYSTEM_PROCESSOR}-linux-ohos")
include_directories(
  "${OHOS_SDK_ROOT}/sysroot/usr/include"
  "${ROS2_HOST_DIR}/include"
  "${ARCH_SPECIFIC_INCLUDE}"
)

# 指定三方库路径修复
set(Python3_ROOT_DIR ${CMAKE_SYSROOT}/vendor)
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} "${CMAKE_SYSROOT}/vendor/opencv/lib/cmake/opencv4")
set(Eigen3_INCLUDE_DIRS "${CMAKE_SYSROOT}/usr/include/eigen3")
set(EIGEN3_INCLUDE_DIRS "${Eigen3_INCLUDE_DIRS}")


# 禁用test/example
# TBB
set(TBB_TEST OFF)
# g2o
set(G2O_BUILD_EXAMPLES OFF)
# ceres-solver
set(BUILD_TESTING OFF)
set(BUILD_EXAMPLES OFF CACHE BOOL "build examples off" FORCE)
set(BUILD_BENCHMARKS OFF CACHE BOOL "build benchmarks off" FORCE)

# 调试输出
message(STATUS "OHOS_SDK_ROOT: ${OHOS_SDK_ROOT}")
message(STATUS "CMAKE_SYSROOT: ${CMAKE_SYSROOT}")
message(STATUS "CMAKE_C_FLAGS: ${CMAKE_C_FLAGS}")
message(STATUS "CMAKE_EXE_LINKER_FLAGS: ${CMAKE_EXE_LINKER_FLAGS}")
set(CMAKE_VERBOSE_MAKEFILE ON)
